package thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo28 {
    public static void main(String[] args) {
        //生产者线程与消费者线程
        BlockingQueue<Integer> queue=new LinkedBlockingQueue<>(1000);
        Thread producer=new Thread(()->{
            int n=0;
            while(true){
                try {
                    queue.put(n);
                    System.out.println("生产者："+n);
                    n++;
                    //Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"producer");
        Thread consumer=new Thread(()->{
            while(true){
                try {
                    int n=queue.take();
                    Thread.sleep(1000);
                    System.out.println("消费者："+n);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"consumer");
        producer.start();
        consumer.start();
    }
}
